//
//  StatuesView.m
//  SinaWeibo
//
//  Created by Apple on 16/8/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "StatuesView.h"
#import "PhotoCell.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@interface StatuesView() <UICollectionViewDataSource,UICollectionViewDelegate>


@end

@implementation StatuesView


#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.statueModel.pic_urls.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    
    //设置数据
    cell.pictureModel = self.statueModel.pic_urls[indexPath.row];
    
    return cell;
    
}


#pragma mark --- UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //1.创建浏览器
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc]init];
    
    //2.告诉浏览器腰显示什么内容,将需要显示的图片数组转化为MJ数组，用循环将每个转化好的模型对象添加到数组中
    
    NSMutableArray *photoArray = [NSMutableArray array];
    
    for (int i=0; i<self.statueModel.pic_urls.count; i++) {
        
        //2.0取出每一个cell
        NSIndexPath *path = [NSIndexPath indexPathForItem:i inSection:0];
        PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:path];
        
        //2.1 创建浏览器需要的模型对象
        MJPhoto *photo = [[MJPhoto alloc]init];
        
        //2.2 告诉模型对象要显示的图片地址
        NSURL *photoURL = [NSURL URLWithString:cell.pictureModel.bmiddle_pic];
        
        photo.url = photoURL;
        
        //2.3 告诉模型对象显示的图片对应的原始缩略图的哪张
        
        photo.srcImageView = cell.pictureView;
        
        //将对象保存到数组中并且告诉数组中
        [photoArray addObject:photo];
        
    }
    
    
    browser.photos = photoArray;
    
    //3.告诉浏览器当前图片索引
    browser.currentPhotoIndex = indexPath.item;
    
    //4.显示
    [browser show];
    
}


@end
